package demo11_ThreadPool;

import java.util.concurrent.*;

/**
 * @author kunkun
 * @className Test01_ThreeMethods
 * @Description Executors的三大方法
 * @date 2025/3/6 16:34
 */
public class Test02_ParameterType {
    public static void main(String[] args) {
        //1. 创建线程池
        ThreadPoolExecutor service = new ThreadPoolExecutor(
            2,5,3,
                TimeUnit.SECONDS,
                new LinkedBlockingDeque<Runnable>(3),
                Executors.defaultThreadFactory(),
//                new ThreadPoolExecutor.AbortPolicy()        //不处理，抛出异常，RejectedExecutionException
//                new ThreadPoolExecutor.CallerRunsPolicy()   //不处理，哪里来的返回哪里
//                new ThreadPoolExecutor.DiscardPolicy()      //直接舍弃，不抛出异常
                new ThreadPoolExecutor.DiscardOldestPolicy()    //和存在时间最久的进行资源竞争，若竞争不过，直接舍弃
        );

        //2. 线程执行
        try {
            for (int i = 0; i < 10; i++) {
                final int index = i;
                service.execute(()->{
                    System.out.println(Thread.currentThread().getName()+"-->"+index);
                });
            }
        } finally {
            //3. 关闭线程池
            service.shutdown();
        }

    }
}
